STATS 32 Session 4: Data Visualization (continued)

Kenneth Tay

Oct 3, 2019

Final project

Goal: Demonstrate that you know how to do data analysis in R

Can be done individually or in a pair.

Minimum requirements:

Project proposal

Recap of session 3

3 essential elements of graphics: data, geometries, aesthetics

Data: Dataset we are using for the plot

##     mpg weight cylinders
## 1  21.0  2.620         6
## 2  21.0  2.875         6
## 3  22.8  2.320         4
## 4  21.4  3.215         6
## 5  18.7  3.440         8
## 6  18.1  3.460         6
## 7  14.3  3.570         8
## 8  24.4  3.190         4
## 9  22.8  3.150         4
## 10 19.2  3.440         6

3 essential elements of graphics: data, geometries, aesthetics

Geometries: Visual elements used for our data

Geom: point

3 essential elements of graphics: data, geometries, aesthetics

Aesthetics: Defines the data columns which affect various aspects of the geom

3 different aesthetics:

Examples of other aesthetics

Examples of other aesthetics

Agenda for today

Layers: Combining multiple plots into one graphic

We can have more than one layer in a graphic.

= +

Each layer contains (essentially):

ggplot2 code

ggplot() +
    geom_boxplot(data = df, mapping = aes(x = cylinders, y = mpg)) +
    geom_point(data = df, mapping = aes(x = cylinders, y = mpg), 
               position = "jitter")

ggplot2 code

When layers share attributes, we only have to type them once:

ggplot(data = df, mapping = aes(x = cylinders, y = mpg)) +
    geom_boxplot() +
    geom_point(position = "jitter")

ggplot2 code

ggplot(df, aes(x = cylinders, y = mpg)) +
    geom_boxplot() +
    geom_point(position = "jitter")

Scales

Examples of scales (Source: A Layered Grammar of Graphics)

Scales example: colors

Default colors

Manually chosen colors

Scales example: x- & y-axes

Default axis limits

Manually chosen axis limits

Facets

Themes

Refers to all non-data ink

ggplot2’s default theme

Minimal theme

More pre-set themes

Classic theme

Dark theme

We’ve only scratched the surface!

R Graph Gallery: an excellent source of inspiration and code snippet examples

Today’s dataset: Diamonds

What makes an expensive diamond?
(Source: USA TODAY)









Optional material

Full specification of a graphic

One graphic contains:

Other grammatical elements: statistics

Behind the scenes, R may need to do some transformation on the dataset to make the graphic.

Other grammatical elements: position

Sometimes we need to tweak the position of the geometric elements because they obscure each other.

Only 9 data points??

Much better

Shapes in R

Colors in R

Color scales in R